<?php
/**
 * Created by PhpStorm.
 * User: ttl
 * Date: 2019/5/18
 * Time: 16:58
 */

namespace app\api\controller;

use app\common\logic\UserLogic;
use think\Db;
use think\Validate;

class User extends Base
{

    /*
    * 初始化操作
    */
    public function _initialize()
    {
        $this->nocheckArr = ['XcxGetOpenid','xcxgetopenid','add_opinion'];
        parent::_initialize();
    }

    //小程序获得openid
    public function XcxGetOpenid(){
        $u_logic = new UserLogic();
        $result = $u_logic->user_getopenid();
        $this->returnMsg($result['status'],$result['msg'],$result['result']);
    }

    /**
     * 授权后更新用户信息
     */
    public function update_user(){
        $u_logic = new UserLogic();
        $result = $u_logic->update_user($this->user);
        $this->returnMsg($result['status'],$result['msg'],$this->user);
    }

    /**
     * 添加意见
     */
    public function add_opinion()
    {
        $add_data = I('.');
        unset($add_data['token']);

        $rule = [
            'names' => 'require|length:1,50',
            'telephone' => 'require|length:7,20',
            'content' => 'length:0,255',
        ];

        $msg = [
            'names.require' => '请输入您的名字',
            'names.length' => '您的名字过长',
            'telephone.require' => '请输入您的电话号码',
            'telephone.length' => '您的电话号码格式有误',
            'content.length' => '您的描述过长！',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $add_data['add_time'] = date("Y-m-d H:i:s",time());
        $add_data['ips'] = request()->ip();
        Db::name('opinion')->insert($add_data);
        $this->returnMsg(1, '添加成功');
    }

}